import asyncio
import functools


def callback(arg, *, kwarg='default'):
    print('callback with {0} and {1}'.format(arg, kwarg))

# call_soon 支持在下一次事件循环的迭代中执行提供的回调函数。回调函数只能传递位置参数，如果想指定关键字参数的话，可以使用 functools.partial 函数来辅助使用。
async def main(loop):
    print('register callbacks')
    loop.call_soon(callback, 1)
    wrapped = functools.partial(callback, kwarg='not default')
    loop.call_soon(wrapped, 2)

    await asyncio.sleep(0.1)


if __name__ == '__main__':
    event_loop = asyncio.get_event_loop()
    try:
        print('entering event loop')
        event_loop.run_until_complete(main(event_loop))
    finally:
        print('closing event loop')
        event_loop.close()